#include "CardType.h"
#include "PokerData.h"
#include "CardTypeData.h"
#include "CardTypeManager.h"
#include <iostream>

ThreeOfAKind::ThreeOfAKind() {
}

ThreeOfAKind::~ThreeOfAKind() {
}

CardTypeData ThreeOfAKind::getCardType(const PokerData & data) const{
  vector<Poker> pokers = data.getPokers();
  int count = pokers.size();
  bool three = false;
  int value = 0;
  if (count > 4) {
    PokerData::sortData(pokers);
    for (int i = 0; i < count - 2; i++) {
      if (pokers.at(i).value == pokers.at(i+2).value) {
        three = true;
        value = pokers.at(i).value;
        break;
      }
    }
    if (three) {
      CardTypeData cardTypeData;
      cardTypeData.type = CardType::ThreeOfAKind;
      cardTypeData.first = value;
      for (int i = 0; i < count; i++) {
        const Poker & p = pokers.at(i);
        if (p.value != value) {
          cardTypeData.second = p.value;
          break;
        }
      }
      for (int i = 0; i < count; i++) {
        const Poker & p = pokers.at(i);
        if (p.value != cardTypeData.first && p.value != cardTypeData.second) {
          cardTypeData.third = p.value;
          break;
        }
      }
      return cardTypeData;
    }
  }
  return getNextType()->getCardType(data);
}

const CardType * ThreeOfAKind::getNextType() const {
  return CardTypeManager::sharedCardTypeManager()->getCardType(CardType::TwoPair);
}
